package com.briup.shopadmin.web.controller;

import com.briup.shopadmin.common.Result;
import com.briup.shopadmin.common.bean.Category;
import com.briup.shopadmin.common.vo.CategoryVO;
import com.briup.shopadmin.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类管理相关接口")
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    ICategoryService categoryService;

    @GetMapping("findCategoryInfo")
    @ApiOperation("查询全部分类信息")
    public Result findCategoryInfo() {
        List<CategoryVO> list = categoryService.findAll();
        return Result.success(list);
    }

    @GetMapping("findCategorysOneLevel")
    @ApiOperation("查询所有的一级分类")
    public Result findCategorysOneLevel() {
        List<Category> list = categoryService.findCategorysOneLevel();
        return Result.success(list);
    }

    @ApiOperation("查询一级分类对应的所有二级分类")
    @GetMapping("findCategoryByOneLevel/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "一级分类编号")
    })
    public Result findCategoryByOneLevel(@PathVariable long id) {
        List<Category> list = categoryService.findCategoryByOneLevel(id);
        return Result.success(list);
    }

    @ApiOperation("添加/修改分类信息")
    @PostMapping("addCategory")
    public Result addCategory(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success();
    }

    @ApiOperation("根据分类名称检索")
    @GetMapping("findByName")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "分类名称")
    })
    public Result findByName(String name) {
        categoryService.findByName(name);
        return Result.success();

    }

    @DeleteMapping("{id}")
    @ApiOperation("根据id删除分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类编号")
    })
    public Result deleteById(@PathVariable Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }
}
